﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace cds_betadev01.source
{
    class Obj
    {
        //constructors
        public Obj()
        {
            World = Matrix.Identity;
        }
        public Obj(Vector3 position, float scale, Vector3 rot)
        {
            World = Matrix.Identity;
            World *= Matrix.CreateScale(scale);
            World *= Matrix.CreateRotationX(rot.X);
            World *= Matrix.CreateRotationY(rot.Y);
            World *= Matrix.CreateRotationZ(rot.Z);
            World *= Matrix.CreateTranslation(position);
            
        }
        protected virtual void init(Vector3 position, float scale, Vector3 rot)
        {
            World = Matrix.Identity;
            World *= Matrix.CreateScale(scale);
            World *= Matrix.CreateRotationX(rot.X);
            World *= Matrix.CreateRotationY(rot.Y);
            World *= Matrix.CreateRotationZ(rot.Z);
            World *= Matrix.CreateTranslation(position);
            
        }
        //attributs
        protected Matrix World;
        private Vector3 position;
        private int index;
        //methods
         //acess data
        public void setPosition(Vector3 p)
        {
            World.Translation = p;

        }
        public void setWorld(Matrix wworld)
        {
            World = wworld;
        }
        public void setWorld(Vector3 position, float scale, Vector3 rot)
        {
            World = Matrix.Identity;
            World.M11 *= scale;
            World.M22 *= scale;
            World.M33 *= scale;
            World *= Matrix.CreateRotationX(rot.X);
            World *= Matrix.CreateRotationY(rot.Y);
            World *= Matrix.CreateRotationZ(rot.Z);
            World.Translation = position;
        }
        public Matrix getWorld()
        {
            return World;
        }

        public Vector3 getPosition()
        {

            return World.Translation; 
        }
        
         //modif
        
          virtual public void upload()
         {
            
         }
         public void transform(Matrix w) 
         {
             World *= w;
         }
         public virtual void translate(Vector3 v)
         {
             World.Translation += v;

         }
         public virtual void rotate(Vector3 angle)
         {
             if (angle.X != 0)
                 World = Matrix.CreateRotationX(angle.X) * World;
             if (angle.Y != 0)
                 World = Matrix.CreateRotationY(angle.Y) * World;
             if (angle.Z != 0)
                 World = Matrix.CreateRotationZ(angle.Z) * World;

         }
         protected void uploadPosition()
         {
             position = World.Translation;
         }
         protected void uploadWorld()
         {
             World.Translation = position;
         }
         public void rotate(Vector3 centre, Vector3 angle)
         {  
             Vector3 gp = getPosition();
             this.translate(new Vector3(centre.X - gp.X, centre.Y - gp.Y, centre.Z - gp.Z));
             this.rotate(angle);
             this.translate(new Vector3(-centre.X+gp.X, -centre.Y+gp.Y,-centre.Z+gp.Z));
         }
         public static Vector3 vect(float forward, float right, float up)
         {
             return new Vector3(right, up, -forward);
         }
    }
}
